package ncov.web;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wf.captcha.utils.CaptchaUtil;
import ncov.model.LocalAuth;
import ncov.model.Result;
import ncov.model.Student;
import ncov.service.LocalAuthService;
import ncov.service.StudentService;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author WuChangJian
 * @date 2020/6/7 10:02
 */
public class LocalAuthAction extends ActionSupport implements ModelDriven<LocalAuth> {
    private Logger logger = Logger.getLogger(LocalAuthAction.class);
    private LocalAuth localAuth = new LocalAuth();
    private Result result;
    @Autowired
    private StudentService studentService;
    @Autowired
    private LocalAuthService localAuthService;

    public String captcha() throws IOException {
        CaptchaUtil.out(ServletActionContext.getRequest(), ServletActionContext.getResponse());
        return SUCCESS;
    }

    /**
     * 登录
     * 登录校验，并将用户相关信息放入session
     */
    public String login() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        logger.info("LocalAuth-->" + localAuth);
        result = new Result();
        if (localAuth.getRole() != null) {
            // 验证码校验
            if (!CaptchaUtil.ver(localAuth.getVerCode(), request)) {
                CaptchaUtil.clear(request);  // 清除session中的验证码
                result.setSuccess(false);
                result.setErrMsg("验证码错误");
                return SUCCESS;
            }
            // 后台登录
            LocalAuth localAuth$ = localAuthService.getOneByNameAndPwd(localAuth);
            if (localAuth$ != null) {
                session.setAttribute("localAuth", localAuth$);
                result.setSuccess(true);
            } else {
                result.setSuccess(false);
                result.setErrMsg("用户名或密码错误！");
            }
        } else {
            // 学生端登录
            Student student = studentService.getStudent(localAuth.getUsername());
            // 懒加载JSON数据存在转化问题，手动解除关联
            if (student != null) {
                student.setReportRecord(null);
                session.removeAttribute("student");
                session.setAttribute("student", student);
                Object o = JSON.toJSON(student);
                result.setSuccess(true);
                result.setData(o);
            } else {
                result.setSuccess(false);
                result.setErrMsg("学生信息未被系统录入，请联系管理员录入！");
            }
        }
        return SUCCESS;
    }

    /**
     * 登出
     */
    public String logout() {
        HttpSession session = ServletActionContext.getRequest().getSession();
        session.removeAttribute("localAuth");
        return SUCCESS;
    }

    /**
     * 修改密码
     */
    public String updatePassword() {
        HttpSession session = ServletActionContext.getRequest().getSession();
        LocalAuth localAuth = (LocalAuth) session.getAttribute("localAuth");
        localAuth.setPassword(this.localAuth.getPassword());
        LocalAuth newLocalAuth = localAuthService.update(localAuth);
        result = new Result();
        result.setData(newLocalAuth);
        result.setSuccess(true);
        return SUCCESS;
    }

    @Override
    public LocalAuth getModel() {
        return localAuth;
    }

    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }

}
